import 'package:animations/animations.dart';
import 'package:emby/model/api_response.dart';
import 'package:emby/model/media.dart';
import 'package:emby/model/media_folder.dart';
import 'package:emby/pages/video/detail.dart';
import 'package:emby/util/function.dart';
import 'package:emby/widget/cupertino_image.dart';
import 'package:flutter/material.dart';

class IndexPage extends StatefulWidget {
  @override
  _IndexPageState createState() => _IndexPageState();
}

class _IndexPageState extends State<IndexPage> {
  List<Media> medias = [];
  List<Media> resumes = [];
  List<MediaFolder> mediaFolders = [];
  @override
  void initState() {
    getData();
    super.initState();
  }

  getData() async {
    Api.userLatestMedia(limit: 16, fields: "PrimaryImageAspectRatio,BasicSyncInfo,ProductionYear,Status,EndDate").then((res) {
      if (res.code == 1) {
        setState(() {
          medias = res.data;
        });
      } else if (res.code == 401) {
        Util.toast("登录失效，请重新登录");
//      Navigator.of(context).pushNamedAndRemoveUntil("/loginPage", (route) => false);
      }
    });
    Api.userLibraryViews().then((res) {
      if (res.code == 1) {
        setState(() {
          mediaFolders = res.data;
        });
      } else if (res.code == 401) {
        Util.toast("登录失效，请重新登录");
//      Navigator.of(context).pushNamedAndRemoveUntil("/loginPage", (route) => false);
      }
    });
    Api.userResume(limit: 16, fields: "PrimaryImageAspectRatio,BasicSyncInfo,ProductionYear,Status,EndDate").then((res) {
      if (res.code == 1) {
        setState(() {
          resumes = res.data;
        });
      } else if (res.code == 401) {
        Util.toast("登录失效，请重新登录");
//      Navigator.of(context).pushNamedAndRemoveUntil("/loginPage", (route) => false);
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("首页"),
      ),
      body: ListView(
        padding: EdgeInsets.all(10),
        children: <Widget>[
          Text(
            "我的媒体",
            style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
          ),
          SizedBox(
            height: 5,
          ),
          Container(
            height: 120,
            child: ListView(
              scrollDirection: Axis.horizontal,
              children: mediaFolders.map((folder) {
                return Container(
                  width: 170,
                  child: Column(
                    children: <Widget>[
                      Padding(
                        padding: const EdgeInsets.all(5),
                        child: Container(
                          decoration: BoxDecoration(
                            boxShadow: [
                              BoxShadow(color: Colors.grey, blurRadius: 4),
                            ],
                          ),
                          child: CupertinoExtendedImage(
                            "${Util.baseUrl}/Items/${folder.id}/Images/Primary",
                            width: 160,
                            height: 90,
                            fit: BoxFit.cover,
                            boxShape: BoxShape.rectangle,
                            borderRadius: BorderRadius.circular(5),
                          ),
                        ),
                      ),
                      Text(
                        folder.name,
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                        style: TextStyle(color: Colors.black, fontSize: 14),
                      ),
                    ],
                  ),
                );
              }).toList(),
            ),
          ),
          if (resumes.length > 0) ...[
            SizedBox(
              height: 20,
            ),
            Text(
              "继续观看",
              style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
            ),
            SizedBox(
              height: 5,
            ),
            Container(
              height: 156,
              child: ListView(
                scrollDirection: Axis.horizontal,
                children: resumes.map((media) {
                  return GestureDetector(
                    onTap: () {
                      Navigator.of(context).push(MaterialPageRoute(builder: (context) {
                        return MediaDetail(media);
                      }));
                    },
                    child: Container(
                      width: 210,
                      child: Column(
                        children: <Widget>[
                          Padding(
                            padding: const EdgeInsets.all(5),
                            child: Container(
                              decoration: BoxDecoration(
                                boxShadow: [
                                  BoxShadow(color: Colors.grey, blurRadius: 4),
                                ],
                              ),
                              child: CupertinoExtendedImage(
                                "${Util.baseUrl}/Items/${media.id}/Images/Backdrop",
                                width: 200,
                                height: 112,
                                fit: BoxFit.cover,
                                boxShape: BoxShape.rectangle,
                                borderRadius: BorderRadius.circular(5),
                              ),
                            ),
                          ),
                          Text(
                            media.name,
                            maxLines: 1,
                            overflow: TextOverflow.ellipsis,
                            style: TextStyle(color: Colors.black, fontSize: 14),
                          ),
                          Text(
                            "${media.productionYear ?? ""}",
                            style: TextStyle(color: Colors.grey, fontSize: 12),
                          ),
                        ],
                      ),
                    ),
                  );
                }).toList(),
              ),
            ),
          ],
          if (medias.length > 0) ...[
            SizedBox(
              height: 20,
            ),
            Text(
              "最近添加",
              style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
            ),
            SizedBox(
              height: 5,
            ),
            Container(
              height: 204,
              child: ListView(
                scrollDirection: Axis.horizontal,
                children: medias.map((media) {
//                  return OpenContainer(
//                    transitionType: ContainerTransitionType.fadeThrough,
//                    openBuilder: (BuildContext context, VoidCallback _) {
//                      return MediaDetail(media);
//                    },
//                    closedElevation: 0,
//                    closedShape: const RoundedRectangleBorder(
//                      borderRadius: BorderRadius.all(
//                        Radius.circular(5),
//                      ),
//                    ),
//                    closedColor: Colors.white,
//                    closedBuilder: (BuildContext context, VoidCallback openContainer) {
//                      return Container(
//                        width: 120,
//                        child: Column(
//                          children: <Widget>[
//                            Padding(
//                              padding: const EdgeInsets.all(5),
//                              child: Container(
//                                decoration: BoxDecoration(boxShadow: [
//                                  BoxShadow(color: Colors.grey, blurRadius: 4),
//                                ]),
//                                child: CupertinoExtendedImage(
//                                  "${Util.baseUrl}/Items/${media.id}/Images/Primary",
//                                  width: 110,
//                                  height: 160,
//                                  fit: BoxFit.cover,
//                                  boxShape: BoxShape.rectangle,
//                                  borderRadius: BorderRadius.circular(5),
//                                ),
//                              ),
//                            ),
//                            Text(
//                              media.name,
//                              maxLines: 1,
//                              overflow: TextOverflow.ellipsis,
//                              style: TextStyle(color: Colors.black, fontSize: 14),
//                            ),
//                            Text(
//                              "${media.productionYear ?? ""}",
//                              style: TextStyle(color: Colors.grey, fontSize: 12),
//                            ),
//                          ],
//                        ),
//                      );
//                    },
//                  );
                  return GestureDetector(
                    onTap: () {
                      Navigator.of(context).push(MaterialPageRoute(builder: (context) {
                        return MediaDetail(media);
                      }));
                    },
                    child: Container(
                      width: 120,
                      child: Column(
                        children: <Widget>[
                          Padding(
                            padding: const EdgeInsets.all(5),
                            child: Container(
                              decoration: BoxDecoration(boxShadow: [
                                BoxShadow(color: Colors.grey, blurRadius: 4),
                              ]),
                              child: CupertinoExtendedImage(
                                "${Util.baseUrl}/Items/${media.id}/Images/Primary",
                                width: 110,
                                height: 160,
                                fit: BoxFit.cover,
                                boxShape: BoxShape.rectangle,
                                borderRadius: BorderRadius.circular(5),
                              ),
                            ),
                          ),
                          Text(
                            media.name,
                            maxLines: 1,
                            overflow: TextOverflow.ellipsis,
                            style: TextStyle(color: Colors.black, fontSize: 14),
                          ),
                          Text(
                            "${media.productionYear ?? ""}",
                            style: TextStyle(color: Colors.grey, fontSize: 12),
                          ),
                        ],
                      ),
                    ),
                  );
                }).toList(),
              ),
            ),
          ],
        ],
      ),
//      floatingActionButton: FloatingActionButton(
//        child: Icon(Icons.refresh),
//        onPressed: getData,
//      ),
    );
  }
}
